home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / processes / signaturetoapp / launch.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.4 KB  |  122 lines

  1. /*
  2.     File:        Launch.c
  3.  
  4.     Contains:    This is a little demo that shows how to call SignatureToApp.
  5.                 Usage:
  6.                     Launch ( [-proc] [-file] [-launch] signature )*
  7.                     where signature is a 4-character application signature.
  8.     
  9.                 Default behavior is to launch or bring to the front the application with that signature.
  10.                 Leading flags will modify this:
  11.                     -proc means only to look for a running process with that signature.
  12.                     -file means to find a running process or a file, but not launch it
  13.                     -launch (the default) means to find a running process or find and launch an app. 
  14.                 You can list more than one signature. Each flag modifies the behavior for following
  15.                 signatures until another flag is found.
  16.  
  17.     Written by: Jens Alfke    
  18.  
  19.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  20.  
  21.                 You may incorporate this Apple sample source code into your program(s) without
  22.                 restriction. This Apple sample source code has been provided "AS IS" and the
  23.                 responsibility for its operation is yours. You are not permitted to redistribute
  24.                 this Apple sample source code as "Apple sample source code" after having made
  25.                 changes. If you're going to re-distribute the source, we require that you make
  26.                 it clear in the source that the code was descended from Apple sample source
  27.                 code, but that you've made changes.
  28.  
  29.     Change History (most recent first):
  30.                 7/27/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  31.                 
  32.  
  33. */
  34.  
  35. #include <stdio.h>
  36. #include <console.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39.  
  40. #include <Errors.h>
  41. #include <Memory.h>
  42.  
  43. #include "SignatureToApp.h"
  44.  
  45. int main( int argc, char *argv[] )
  46. {    
  47.     Boolean foundSignature = false;
  48.     short arg;
  49.     Sig2App_Mode mode = Sig2App_LaunchApplication;
  50.     char *argstr;
  51.     OSType sig;
  52.     ProcessSerialNumber psn;
  53.     FSSpec file;
  54.     Boolean launched;
  55.     OSErr err;
  56.     long len;
  57.     
  58.     argc=ccommand(&argv);
  59.     
  60.     if( argc<=1 ) {
  61.         puts("Launch: Finds and/or launches application with given signature");
  62.         return 1;
  63.     }
  64.     
  65.     for( arg=1; arg<argc; arg++ ) {
  66.         argstr = argv[arg];
  67.         
  68.         // Interpret a flag:
  69.         
  70.         if( argstr[0] == '-' ) {
  71.             if( strcmp(argstr,"-proc")==0 )
  72.                 mode = Sig2App_FindProcess;
  73.             else if( strcmp(argstr,"-file")==0 )
  74.                 mode = Sig2App_FindApplication;
  75.             else if( strcmp(argstr,"-launch")==0 )
  76.                 mode = Sig2App_LaunchApplication;
  77.             else {
  78.                 fprintf(stderr,"Launch: Unknown flag “%s”\n",argstr);
  79.                 return 1;
  80.             }
  81.             continue;
  82.         } else {
  83.         
  84.             // Interpret anything else as an application signature:
  85.             
  86.             foundSignature = true;
  87.             sig = '    ';
  88.             len = strlen(argstr);
  89.             if( len>4 )
  90.                 len = 4;
  91.             BlockMove(argstr,&sig,len);                // Copy signature
  92.             
  93.             // Here we go. Watch carefully:
  94.             
  95.             err = SignatureToApp(sig, &psn, &file, &launched, mode, launchContinue);
  96.             
  97.             if( err==fnfErr ) {
  98.                 fprintf(stderr,"Launch: No application with signature '%.4s' found.\n", &sig);
  99.                 return 1;
  100.             } else if( err==procNotFound ) {
  101.                 fprintf(stderr,"Launch: No application with signature '%.4s' is running.\n", &sig);
  102.                 return 1;
  103.             }else if( err ) {
  104.                 fprintf(stderr,"Launch: Error %d (signature '%.4s').\n", err,&sig);
  105.                 return 1;
  106.             }
  107.             
  108.             if( mode < Sig2App_LaunchApplication ) {
  109.                 printf("'%.4s': PSN= {%d/%d}; File= {vol=%hd, dir=%d, name=%P}\n",
  110.                                 &sig,
  111.                                 psn.highLongOfPSN,psn.lowLongOfPSN,
  112.                                 file.vRefNum, file.parID, file.name);
  113.             }
  114.         }
  115.     }
  116.     
  117.     if( !foundSignature ) {
  118.         fprintf(stderr,"Launch: Missing application signature\n");
  119.         return 1;
  120.     }
  121.     return 0;
  122. }